home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1453.dms / var1453.adf / Messages / Example1A.c < prev    next >
C/C++ Source or Header  |  1992-05-02  |  3KB  |  111 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM System                  Amiga C Club       */
  7. /* Chapter: Messages                    Tulevagen 22       */
  8. /* File:    Example1A.c                 181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-02                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will create a message port called "NrPort".   */
  21. /* It will then go to sleep and will first wake up when it    */
  22. /* has received a message. It will collect the message, read  */
  23. /* and alter it before it replies and the program terminates. */
  24.  
  25.  
  26.  
  27. #include <exec/types.h>  /* STRPTR         */
  28. #include <exec/ports.h>  /* struct Message */
  29. #include <exec/memory.h> /* MEMF_PUBLIC    */
  30. #include <exec/nodes.h>  /* NT_MESSAGE     */
  31.  
  32.  
  33.  
  34. /* Declare a pointer to our message port: */
  35. struct MsgPort *msgp;
  36.  
  37. /* The message will come in this form: */
  38. struct NrMessage
  39. {
  40.   struct Message SystemMsg;
  41.   int number;
  42. };
  43.  
  44. /* Declare a pointer to the message: */
  45. struct NrMessage *nrmsg;
  46.  
  47.  
  48.  
  49. /* Declare the functions: */
  50. void clean_up();
  51. void main();
  52.  
  53.  
  54.  
  55. void main()
  56. {
  57.   /* Create a message port: (Name "NrPort", priority 0) */
  58.   msgp = (struct MsgPort *) CreatePort( "NrPort", 0 );
  59.  
  60.   /* Check if we have successfully created a port: */ 
  61.   if( !msgp )
  62.     clean_up( "Could not create the message port!" );
  63.  
  64.  
  65.  
  66.   /* Wait for a message to arrive: */
  67.   printf( "A: Waiting for a message to arrive...\n" );
  68.   WaitPort( msgp );
  69.  
  70.  
  71.  
  72.   /* Now one or more messages have arrived, try to collect */
  73.   /* as many messages as possible:                         */
  74.   while( nrmsg = (struct NrMessage *) GetMsg( msgp ))
  75.   {
  76.     /* Read the message: */
  77.     printf( "A: The value we received was: %d\n", nrmsg->number );
  78.     printf( "A: Lets change it!\n" );
  79.  
  80.     /* Alter it: */
  81.     nrmsg->number = 67890;
  82.  
  83.     /* Reply: */
  84.     ReplyMsg( nrmsg );
  85.  
  86.     /* Note that after we have replied, we may not */
  87.     /* read or alter the message any more!         */
  88.   }
  89.  
  90.  
  91.  
  92.   clean_up( "The End!" );
  93. }
  94.  
  95.  
  96.  
  97. void clean_up( text )
  98. STRPTR text;
  99. {
  100.   /* If we have successfully created a port, close it: */ 
  101.   if( msgp )
  102.     DeletePort( msgp);
  103.  
  104.   /* Print any message: */
  105.   printf( "A: %s\n", text );
  106.  
  107.   /* Quit: */
  108.   exit( 0 );
  109. }
  110.  
  111.